package com.sakiko.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * projectName: com.sakiko.listener
 *
 * @author: Yang
 * description: 死信队列
 * time: 2025/8/17 20:48
 */
@Component
@Slf4j
public class DeadLetterExchangeController {
    public static final String NORMAL_EXCHANGE_DIRECT = "exchange.normal.video";
    public static final String DEAD_EXCHANGE_DIRECT = "exchange.dead.letter.video";
    public static final String NORMAL_ROUTING_KEY = "routing.key.normal.video";
    public static final String DEAD_ROUTING_KEY = "routing.key.dead.letter.video";
    public static final String NORMAL_QUEUE_NAME = "queue.normal.video";
    public static final String DEAD_QUEUE_NAME = "queue.dead.letter.video";

    // 监听正常队列 -- 消费者 1
    @RabbitListener(queues = {NORMAL_QUEUE_NAME})
    public void processMessage(String dataString, Message message, Channel channel) throws Exception {
        // 监听正常队列，但是我拒绝
        log.info("[normal message] だが断る！");
        channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
    }

    // 监听死信队列 -- 消费者 2
    @RabbitListener(queues = {DEAD_QUEUE_NAME})
    public void processMessageDead(String dataString, Message message, Channel channel) throws Exception {
        // 监听死信队列
        log.info("[dead letter] dataString = " + dataString);
        log.info("[dead letter] 我是死信监听方法，我接收到了死信消息");
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}
